﻿using AbpApplication.AppServices.Dto;
using AbpEntityFrameworkcore.DbContext;
using AbpToDomain.CustomEntityInfo;
using AbpToDomain.EntityInfo;
using Application.Expand;
using Mesosphere.InPut;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace AbpApplication.AppServices
{
    public class PdaService : ApplicationService, IPdaService
    {
        private readonly CustomDbContext _customDbContext;
        private readonly MyDbContext _myDbContext;
        private readonly IRepository<View_Packing_List> View_Packing_List_rep;
        private readonly IRepository<AGV_MPNVSPartnumber> AGV_MPNVSPartnumber_rep;
        private readonly IRepository<AGV_Packing_List_Verification> AGV_Packing_List_verification_rep;
        private readonly IRepository<AGV_Reel_Bonding> AGV_Reel_Bonding_rep;
        private readonly IRepository<View_toAGV_Reel> View_toAGV_Reel_rep;
        private readonly IRepository<View_ToAGV_MatList> View_toAGV_MatList_rep;
        private readonly IRepository<View_ToAGV_MatList> View_ToAgv_MatList_rep;
        private readonly IRepository<AGV_Kitted> agv_Kitted_rep;
        private readonly IRepository<MaterialBase> materialbase_rep;
        private readonly IRepository<Box> box_rep;
        private readonly IRepository<BoxPosition> box_position_rep;
        private readonly IRepository<Unusual> Unusual_rep;

        public PdaService(CustomDbContext customDbContext, IRepository<View_Packing_List>
            view_packing_list_res,
            IRepository<AGV_MPNVSPartnumber> aGV_MPNVSPartnumber_rep,
            IRepository<AGV_Packing_List_Verification> aGV_Packing_List_verification_rep,
            IRepository<AGV_Reel_Bonding> aGV_Reel_Bonding_rep,
            IRepository<View_toAGV_Reel> view_toAGV_Reel_rep,
            IRepository<View_ToAGV_MatList> view_toAGV_MatList_rep,
            IRepository<View_ToAGV_MatList> view_ToAgv_MatList_rep,
            IRepository<AGV_Kitted> agv_Kitted_rep, IRepository<MaterialBase> materialbase_rep, IRepository<Box> box_rep, IRepository<BoxPosition> box_position_rep, MyDbContext myDbContext, IRepository<Unusual> unusual_rep)
        {
            _customDbContext = customDbContext;
            this.View_Packing_List_rep = view_packing_list_res;
            AGV_MPNVSPartnumber_rep = aGV_MPNVSPartnumber_rep;
            AGV_Packing_List_verification_rep = aGV_Packing_List_verification_rep;
            AGV_Reel_Bonding_rep = aGV_Reel_Bonding_rep;
            View_toAGV_Reel_rep = view_toAGV_Reel_rep;
            View_toAGV_MatList_rep = view_toAGV_MatList_rep;
            View_ToAgv_MatList_rep = view_ToAgv_MatList_rep;
            this.agv_Kitted_rep = agv_Kitted_rep;
            this.materialbase_rep = materialbase_rep;
            this.box_rep = box_rep;
            this.box_position_rep = box_position_rep;
            _myDbContext = myDbContext;
            Unusual_rep = unusual_rep;
        }

        #region 新料入库
        //创建入库
        public async Task<ApiResponse> CreateOrder(PdaInPut inPut)
        {
            View_Packing_List view_Packing_List = await View_Packing_List_rep.FirstOrDefaultAsync(x => x.PartnumberLotID == inPut.PartnumberLotID);
            List<View_Packing_List> view_Packing_List2 =
                await View_Packing_List_rep.GetListAsync(x => x.PackingListNumber_Supplier == inPut.PartnumberLotID && !string.IsNullOrEmpty(x.DateKey));

            if (view_Packing_List != null)
            {
                PdaDto pdadto = ObjectMapper.Map<View_Packing_List, PdaDto>(view_Packing_List);
                pdadto.ParynumberLotType = "海外";
                return new ApiResponse("查询成功", true, pdadto);
            }
            else if (view_Packing_List == null && view_Packing_List2.Count > 0)
            {
                PdaDto2 pdaDto2 = new PdaDto2();
                List<string> DateKeys = view_Packing_List2.Select(x => x.DateKey + "+" + x.BinType).ToList();
                DateKeys = DateKeys.Distinct().ToList();
                DateKeys = DateKeys.OrderByDescending(x => x).ToList();
                pdaDto2.DateKeys = DateKeys;
                pdaDto2.ParynumberLotType = "国内";
                return new ApiResponse("查询成功", true, pdaDto2);
            }
            return new ApiResponse("查询不到的票号", false, null);
        }
        //添加物料
        public async Task<ApiResponse> CreateMaterial(PdaInPut2 inPut)
        {

            //reeltype判断
            string ReelType;
            if (inPut.PositionCode.Length == 9)
            {
                ReelType = "01";//大卷盘
            }
            else
            {
                ReelType = "00";//小卷盘
            }
            //bintype查询
            View_Packing_List view_Packing_Listmode = await _customDbContext.View_Packing_Lists.Where(x => x.PartnumberLotID == inPut.PartnumberLotID &&
               x.PartNumber == inPut.PartNumber).FirstOrDefaultAsync();
            if (view_Packing_Listmode != null)
            {
                inPut.BinType = view_Packing_Listmode.BinType;
            }
            //制造商判断
            if (inPut.MPNCheak == false)
            {
                List<AGV_MPNVSPartnumber> aGV_MPNVSPartnumbers = _customDbContext.AGV_MPNVSPartnumbers.ToList();
                try
                {
                    bool AGVBool = aGV_MPNVSPartnumbers.Any(x => Regex.Replace(inPut.MPN, @"[^0-9a-zA-Z]+", "").
           Contains(Regex.Replace(x.MPN, @"[^0-9a-zA-Z]+", "")) && x.Partnumber == inPut.PartNumber);
                    if (AGVBool == false)
                    {
                        return new ApiResponse("制造商码不符", false, null);
                    }
                }
                catch
                {
                    return new ApiResponse("制造商码匹配错误", false, null);
                }
            }
            //票号查询判断
            View_Packing_List view_Packing_List = new View_Packing_List();
            if (inPut.ParynumberLotType == "海外")
            {
                view_Packing_List = await _customDbContext.View_Packing_Lists.Where(x => x.PartnumberLotID == inPut.PartnumberLotID &&
                                    x.PartNumber == inPut.PartNumber).FirstOrDefaultAsync();
                if (view_Packing_List == null)
                {
                    return new ApiResponse("票号查询失败", false, null);
                }
            }
            else if (inPut.ParynumberLotType == "国内")
            {
                view_Packing_List = await _customDbContext.View_Packing_Lists?.Where(x => x.PackingListNumber_Supplier == inPut.PartnumberLotID &&
                                   x.DateKey.Contains(inPut.DateKey) && x.PartNumber == inPut.PartNumber).FirstOrDefaultAsync();
                if (view_Packing_List == null)
                {
                    return new ApiResponse("票号查询失败", false, null);
                }
            }
            //入库物料数量判断
            List<MaterialBase> MaterialBases = await materialbase_rep.GetListAsync(x => x.PartnumberLotID == inPut.PartnumberLotID +
            inPut.DateKey && x.MaterialCode == inPut.PartNumber && !string.IsNullOrEmpty(x.People));
            long num = (long)MaterialBases.Select(x => x.Quantity).Sum();
            if (num + inPut.Qty > view_Packing_List.Qty)
            {
                return new ApiResponse("入库数量已超票据数量", false, null);
            }
            //料盒判断  reeltype  箱体大小判断    
            Box box = await box_rep.FirstOrDefaultAsync(x => inPut.PositionCode.Contains(x.BoxCode));
            if (box == null)
            {
                return new ApiResponse("未查询到此料盒", false, null);
            }
            //bintype判断
            BoxPosition positionCheck = await _myDbContext.BoxPositions.Include(x => x.MaterialBase).
                            FirstOrDefaultAsync(x => x.BoxPositionCode.Contains(inPut.PositionCode.Substring(0, 8))&&x.MaterialBase!=null);
            if (positionCheck != null&&positionCheck.MaterialBase.BinType!=inPut.BinType)
            {
                return new ApiResponse("该料盒bintype不符合", false, null);
            }
            //查询合适的料盒
            BoxPosition boxPosition=  await box_position_rep.FirstOrDefaultAsync(x => x.BoxPositionCode.Contains( inPut.PositionCode)&&x.MaterialBase==null);
            if (boxPosition == null)
            {
                return new ApiResponse("请检查料盒容量或者是否存在", false, null);
            }
            //create Materilabase
            MaterialBase materialBase=new MaterialBase();
            materialBase.MaterialuniqueCode = ExtendRandom.CreateRandom();
            materialBase.PartnumberLotID = inPut.PartnumberLotID+inPut.DateKey;
            materialBase.PartnumberLotType=inPut.ParynumberLotType;
            materialBase.MvnPartNumber=inPut.MPN;
            materialBase.BinType = inPut.BinType;
            materialBase.MaterialCode = inPut.PartNumber;
            materialBase.PutInDate = DateTime.Now;
            materialBase.CreatedDate=DateTime.Now;
            materialBase.BoxPosition = boxPosition;
            materialBase.Quantity= (int?)inPut.Qty;
            materialBase.ReelType =ReelType;      
            materialBase.Type = "新料入库";
            materialBase.State = "待入库";
            materialBase.People=inPut.People;
            await materialbase_rep.InsertAsync(materialBase);
            // create AGV_Reel_Bonding
            AGV_Reel_Bonding aGV_Reel_Bonding = new AGV_Reel_Bonding();           
            aGV_Reel_Bonding.KLTID = box.BoxCode;
            aGV_Reel_Bonding.SeqID = boxPosition.BoxPositionCode;
            aGV_Reel_Bonding.Qty = inPut.Qty;
            aGV_Reel_Bonding.Partnumber = inPut.PartNumber;
            aGV_Reel_Bonding.BinType = inPut.BinType;
            aGV_Reel_Bonding.PartnumberLotID = inPut.PartnumberLotID+inPut.DateKey;
            aGV_Reel_Bonding.ReelType = ReelType;
            aGV_Reel_Bonding.Remark=materialBase.MaterialuniqueCode;
            aGV_Reel_Bonding.InputDate = materialBase.PutInDate;

            await AGV_Reel_Bonding_rep.InsertAsync (aGV_Reel_Bonding);
            return new ApiResponse("添加成功", true, materialBase);
        }
        //获取待入库物料
        public async Task<ApiResponse> Material_Pda_Last(PdaInPut3 inPut)
        {
            try
            {
                List<MaterialBase> materialbases = await _myDbContext.materialBases.Include(x=>x.BoxPosition).Where(x => x.PartnumberLotID == inPut.PartnumberLotID && 
                                                          x.State == "待入库").ToListAsync();
                MaterialBase materialBase = materialbases.OrderByDescending(x => x.CreatedDate).FirstOrDefault();
                PdaDto3 materialBasedto = ObjectMapper.Map<MaterialBase, PdaDto3>(materialBase);
                return new ApiResponse("查询成功", true, materialBasedto);
            }
            catch (Exception ex)
            {
                return new ApiResponse("查询失败", false, null);
            }
        }
        //确认入库
        public async Task<ApiResponse> AGV_Packing_List_Verification_AddListAsync(PdaInPut4 input)
        {
            List<View_Packing_List> view_Packing_Lists = new List<View_Packing_List>();
            List<MaterialBase> material_Pdas = await materialbase_rep.GetListAsync(x => x.PartnumberLotID == input.PartnumberLotID && x.State == "待入库");
            List<MaterialBase> material_Pdas_ow = await materialbase_rep.GetListAsync(x => x.PartnumberLotID == input.PartnumberLotID);
            view_Packing_Lists = await View_Packing_List_rep.GetListAsync(x => x.PartnumberLotID == input.PartnumberLotID);
            if (material_Pdas.Count == 0)
            {
                return new ApiResponse("没有准备入库的物料或者此工单已入库", false, null);
            }
            if (view_Packing_Lists.Count == 0)
            {
                view_Packing_Lists = await View_Packing_List_rep.GetListAsync(x => x.PackingListNumber_Supplier + x.DateKey == input.PartnumberLotID);
            }
            //差值计算
            var material_pdadiff = material_Pdas_ow.GroupBy(item => item.MaterialCode)
                 .Select(group => new { PartNumber = group.Key, qty = group.Sum(item => item.Quantity) })
                 .ToList();

            var diffItems = view_Packing_Lists.Join(material_pdadiff, a => a.PartNumber, b => b.PartNumber, (a, b) => new { PartNumber = a.PartNumber, Diff = a.Qty - b.qty })
                .Where(x => x.Diff > 0).ToList();
            if (diffItems.Count() == 0)
            {
                List<Unusual> unusuals = await Unusual_rep.GetListAsync(x => x.PartnumberLotID == input.PartnumberLotID);
                foreach (var item in unusuals)
                {
                    item.QtyDiff = 0;
                }
                await Unusual_rep.UpdateManyAsync(unusuals);
            }
            foreach (var item in diffItems)
            {

                Unusual unusual = await Unusual_rep.FindAsync(x => x.PartnumberLotID == input.PartnumberLotID && x.PartNumber == item.PartNumber);
                if (unusual != null)
                {
                    unusual.QtyDiff = item.Diff;
                    await Unusual_rep.UpdateAsync(unusual);
                }
                else
                {
                    Unusual unusual1 = new Unusual();
                    unusual1.PartnumberLotID = input.PartnumberLotID;
                    unusual1.PartNumber = item.PartNumber;
                    unusual1.QtyDiff = item.Diff;
                    await Unusual_rep.InsertAsync(unusual1);
                }
            }
            List<AGV_Packing_List_Verification> AGV_Packing_List_Verifications = ObjectMapper.Map<List<MaterialBase>, List<AGV_Packing_List_Verification>>(material_Pdas);
            await AGV_Packing_List_verification_rep.InsertManyAsync(AGV_Packing_List_Verifications);//insert 1
            //update materialbase
            foreach (var item in material_Pdas)
            {
                item.State = "已入库";
            }
            await materialbase_rep.UpdateManyAsync(material_Pdas);
            //缺少物料入库
            if (material_Pdas.Select(b => b.MaterialCode).Distinct().Count() < view_Packing_Lists.Count)
            {
                List<View_Packing_List> view_Packing_Listsdiff = view_Packing_Lists.Where(x => !material_Pdas_ow.Any(b => b.MaterialCode == x.PartNumber)).ToList();

                List<View_Packing_ListDto> view_Packing_ListDto = ObjectMapper.Map<List<View_Packing_List>, List<View_Packing_ListDto>>(view_Packing_Listsdiff);
                return new ApiResponse("入库成功,缺少物料", true, view_Packing_ListDto);

            }
            //数量异常入库
            if (diffItems.Count() > 0)
            {
                return new ApiResponse("入库成功,有异常数量请检查", true, AGV_Packing_List_Verifications);
            }
            return new ApiResponse("入库成功", true, AGV_Packing_List_Verifications);


        }
        //加入异常
        public async Task<ApiResponse> CreateUnusual(UnusualInput input)
        {
            var model = await Unusual_rep.SingleOrDefaultAsync(x => x.PartNumber == input.PartNumber && x.PartnumberLotID == input.PartnumberLotID);
            if (model != null)
            {
                return new ApiResponse("已有该信息", false, null);
            }

            Unusual unusual = new Unusual();
            unusual.PartNumber = input.PartNumber;
            unusual.PartnumberLotID = input.PartnumberLotID;
            unusual.Remarks = input.Remarks;
            await Unusual_rep.InsertAsync(unusual);
            return new ApiResponse("备注成功", true, null);
        }
        //删除物料
        public async Task<ApiResponse> DeleteMaterial(Guid Id)
        {
            MaterialBase materialBase = await materialbase_rep.FirstOrDefaultAsync(x => x.Id_Materialbase == Id);
            if (materialBase == null)
            {
                return new ApiResponse("查询不到-materialBase", false, null);
            }
            if (materialBase.State == "已入库")
            {
                return new ApiResponse("已确认入库的物料不允许删除", false, null);
            }
            //materialbase delete
            await materialbase_rep.DeleteAsync(materialBase);
            await AGV_Reel_Bonding_rep.DeleteAsync(x=>x.Remark==materialBase.MaterialuniqueCode);
            return new ApiResponse("删除成功", true, null);
        }
        //获取详情
        public async Task<ApiResponse> GetMastList(string productorder)
        {
            List<View_ToAGV_MatList> view_ToAGV_MatLists = await View_toAGV_MatList_rep.GetListAsync(x => x.productorder == productorder);
            List<PdaDto7> pdaDto7s = ObjectMapper.Map<List<View_ToAGV_MatList>, List<PdaDto7>>(view_ToAGV_MatLists);
            return new ApiResponse("获取成功", true, pdaDto7s);
        }
        //获取入库物料
        public async Task<ApiResponse> Material_Pda_GetListAsync(PdaInPut3 inPut)
        {
            try
            {
                List<MaterialBase> materialbases = await _myDbContext.materialBases.Include(x => x.BoxPosition).Where(x => x.PartnumberLotID == inPut.PartnumberLotID).ToListAsync();
                List< PdaDto3> materialBasedto = ObjectMapper.Map<List<MaterialBase>,List<PdaDto3>>(materialbases);
                return new ApiResponse("查询成功", true, materialBasedto);
            }
            catch (Exception ex)
            {
                return new ApiResponse("查询失败", false, null);
            }
        }

        #endregion

        #region 入库检查
        //入库检查
        public async Task<ApiResponse> InHouseCheck(PdaInPut10 pdaInPut)
        {
            List<AGV_Reel_Bonding> aGV_Reel_Bondings = new List<AGV_Reel_Bonding>();
            AGV_Reel_Bonding aGV_Reel_Bonding = await AGV_Reel_Bonding_rep.FirstOrDefaultAsync(x => x.SeqID == pdaInPut.SeqID && x.Partnumber == pdaInPut.PartNumber
            && x.Qty == pdaInPut.Qty && string.IsNullOrEmpty(x.ReadByAGV));
            List<AGV_Reel_Bonding> aGV_Reel_BondingErrs = await AGV_Reel_Bonding_rep.GetListAsync(x => x.SeqID == pdaInPut.SeqID);
            AGV_Reel_Bonding aGV_Reel_BondingErr = aGV_Reel_BondingErrs.OrderByDescending(x => x.Datekey).FirstOrDefault();
            aGV_Reel_BondingErrs.Clear();
            aGV_Reel_BondingErrs.Add(aGV_Reel_BondingErr);
            if (aGV_Reel_BondingErr == null)
            {
                return new ApiResponse("不存在的货位", false, null);
            }

            if (aGV_Reel_Bonding == null && aGV_Reel_BondingErrs.Count > 0)
            {
                List<PdaDto5> pdaDto5errs = ObjectMapper.Map<List<AGV_Reel_Bonding>, List<PdaDto5>>(aGV_Reel_BondingErrs);
                return new ApiResponse("未匹配到该条数据", false, pdaDto5errs);
            }
            aGV_Reel_Bondings.Add(aGV_Reel_Bonding);
            List<PdaDto5> pdaDto5s = ObjectMapper.Map<List<AGV_Reel_Bonding>, List<PdaDto5>>(aGV_Reel_Bondings);
            return new ApiResponse("获取成功", true, pdaDto5s);

        }
        //docnum更新
        public async Task<ApiResponse> Update_DocMentNo(PdaInPut9 input)
        {
            AGV_Reel_Bonding aGV_Reel_Bonding = await AGV_Reel_Bonding_rep.FindAsync(x => x.Id == input.Id);
            if (aGV_Reel_Bonding != null)
            {
                MaterialBase materialbase = await materialbase_rep.FirstOrDefaultAsync(x => x.MaterialuniqueCode == aGV_Reel_Bonding.Remark);
                materialbase.DocumentNo = input.DocumentNo;
                await materialbase_rep.UpdateAsync(materialbase);
                aGV_Reel_Bonding.DocumentNo = input.DocumentNo;
                await AGV_Reel_Bonding_rep.UpdateAsync(aGV_Reel_Bonding);
                return new ApiResponse("修改成功", true, null);
            }
            return new ApiResponse("aGV_Reel_Bonding无结果", false, null);
        }
        //获取格口内已填docnum数量
        public async Task<ApiResponse> GetDocNumNo(string PositionCode)
        {
            List<AGV_Reel_Bonding> aGV_Reel_Bondings = await AGV_Reel_Bonding_rep.GetListAsync(x => x.BinID == PositionCode && x.ReadByAGV == null);
            List<AGV_Reel_Bonding> aGV_Reel_BondingNull = new List<AGV_Reel_Bonding>();
            if (aGV_Reel_Bondings.Count > 0)
            {
                aGV_Reel_BondingNull = aGV_Reel_Bondings.Where(x => string.IsNullOrEmpty(x.DocumentNo) || x.DocumentNo == "NULL").ToList();
            }
            PdaDto9 pdaDto9 = new PdaDto9();
            pdaDto9.Docquantity = aGV_Reel_Bondings.Count;
            pdaDto9.DocquantityNull = aGV_Reel_BondingNull.Count;
            return new ApiResponse(true, pdaDto9);
        }
        //入库检查获取列表
        public async Task<ApiResponse> GetAgv_reelBonding(string BinID)
        {
            try
            {
                List<AGV_Reel_Bonding> aGV_Reel_Bondings = await AGV_Reel_Bonding_rep.GetListAsync(x => x.BinID == BinID);
                List<PdaDto12> pdaDto12s = ObjectMapper.Map<List<AGV_Reel_Bonding>, List<PdaDto12>>(aGV_Reel_Bondings);
                AGV_Reel_Bonding pdaDto = aGV_Reel_Bondings.Where(x => (string.IsNullOrEmpty(x.DocumentNo) || x.DocumentNo == "NULL") && string.IsNullOrEmpty(x.ReadByAGV)).FirstOrDefault();
                if (pdaDto != null)
                {
                    string s = pdaDto.SeqID.Substring(pdaDto.SeqID.LastIndexOf("_") + 1);
                    return new ApiResponse(s, true, pdaDto12s);
                }
                else
                {
                    return new ApiResponse("已检查完", true, pdaDto12s);
                }

            }
            catch (Exception ex)
            {
                return new ApiResponse(ex.ToString(), false, null);
            }
        }
        #endregion



        #region 尾料入箱
        public async Task<ApiResponse> CreateTailList(string PositionCode)
        {
            try
            {
                if (PositionCode.Length < 8)
                {
                    return new ApiResponse("输入字符长度过短", false, null);
                }

                List<AGV_Reel_Bonding> aGV_Reel_Bondings = await AGV_Reel_Bonding_rep.GetListAsync(x => x.KLTID == PositionCode&& string.IsNullOrEmpty(x.ReadByAGV));
                if (aGV_Reel_Bondings.Count == 0)
                {
                    return new ApiResponse("未查询到该料盒尾料", false, null);
                }
                bool inputdatecheck= aGV_Reel_Bondings.Any(x => x.InputDate == null);
                if (inputdatecheck)
                {
                    return new ApiResponse("检查到源物料日期为空", false, null);
                }
                //docnum检查
                bool res = aGV_Reel_Bondings.Any(x => x.Reelnumber == null && x.DocumentNo == null);
                if (res)
                {
                    return new ApiResponse("存在document为空的数据", false, null);
                }
                // Materilalbase create         AGV_Reel_Bonding update
                List<BoxPosition> boxPositions =await box_position_rep.WithDetails().ToListAsync();            
                 boxPositions= boxPositions.Where(x=>aGV_Reel_Bondings.Any(y=>y.SeqID==x.BoxPositionCode)&&x.MaterialBase==null).ToList();
                if(boxPositions.Count!= aGV_Reel_Bondings.Count)
                {
                    return new ApiResponse("箱盒库位数量不符", false, null);
                }
                List<MaterialBase> materialbases = new List<MaterialBase>();
                foreach (var item in aGV_Reel_Bondings)
                {
                    BoxPosition boxPosition=  await box_position_rep.FirstOrDefaultAsync(x => x.BoxPositionCode == item.SeqID);
                    if (boxPosition == null)
                    {
                        return new ApiResponse($"尾料入库-未查询到库位{item.SeqID}", false, null);
                    }
                    MaterialBase materialBase = ObjectMapper.Map<AGV_Reel_Bonding, MaterialBase>(item);
                    materialBase.CreatedDate = DateTime.Now;
                    materialBase.BoxPosition = boxPosition;
                    materialBase.State = "已入库";
                    materialBase.Type = "尾料入库";
                    materialBase.MaterialuniqueCode = ExtendRandom.CreateRandom();
                    materialbases.Add(materialBase);
                    item.ReadDatekey = DateTime.Now;
                    item.ReadByAGV = "Unbonding";
                    item.Remark = materialBase.MaterialuniqueCode;
                }
                await materialbase_rep.InsertManyAsync(materialbases);
                await AGV_Reel_Bonding_rep.UpdateManyAsync(aGV_Reel_Bondings);
                // AGV_Packing_List_Verification create
                //List<AGV_Packing_List_Verification> AGV_Packing_List_Verifications = ObjectMapper.Map<List<AGV_Reel_Bonding>, List<AGV_Packing_List_Verification>>(aGV_Reel_Bondings);
                //await AGV_Packing_List_verification_rep.InsertManyAsync(AGV_Packing_List_Verifications);//insert 1 
                return new ApiResponse("添加成功", true, aGV_Reel_Bondings);
            }
            catch
            {
                return new ApiResponse("请检查源数据是否有重复", false, null);
            }

        }

        #endregion



        #region 
        //获取异常
        public async Task<ApiResponse> Unuasl_GetListAsync()
        {
            List<Unusual> unusuals = await Unusual_rep.GetListAsync(x => (x.Remarks != "正常" && x.Remarks != "退货") && x.QtyDiff != 0);
            List<PdaDto4> Dto = ObjectMapper.Map<List<Unusual>, List<PdaDto4>>(unusuals);
            return new ApiResponse("获取成功", true, Dto);
        }
        //更新异常
        public async Task<ApiResponse> Unuasl_UpdateAsync(PdaInPut7 input)
        {
            Unusual unusual = await Unusual_rep.FindAsync(x => x.Id == input.Id);
            if (unusual != null)
            {
                unusual.Remarks = input.Remarks;
                await Unusual_rep.UpdateAsync(unusual);


                return new ApiResponse("更新成功", true, null);
            }
            return new ApiResponse("更新失败,未查询到", false, null);
        }
        #endregion
    }
}
